(function() {
  var root = this;

  var previousTool = root.Tool;

  var ArrayProto = Array.prototype,
      ObjProto = Object.prototype,
      FuncProto = Function.prototype;

  var Tool = function(obj) {
      if (obj instanceof Tool) return obj;
      if (!(this instanceof Tool)) return new Tool(obj);
      this._tool = obj;
  };
  //是否可以通过exports暴露
  if (typeof exports !== 'undefined') {
      if (typeof module !== 'undefined' && module.exports) {
          exports = module.exports = Tool;
      }
      exports.Tool = Tool;
  } else {
      root.Tool = Tool;
  };

  /**
   * cookie operation
   **/
  Tool.cookie = {
    set: function (objName, objValue, objHours) {
        var str = objName + "=" + escape(objValue);
        if (objHours > 0) {//为0时不设定过期时间，浏览器关闭时cookie自动消失
            var date = new Date();
            var ms = objHours * 3600 * 1000;
            date.setTime(date.getTime() + ms);
            str += "; expires=" + date.toGMTString() + "; path=/";
        }
        document.cookie = str;
    },
    get: function (objName) {
        var arrStr = document.cookie.split("; ");
        for (var i = 0; i < arrStr.length; i++) {
            var temp = arrStr[i].split("=");
            if (temp[0] == objName)
                return unescape(temp[1]);
        }
    },
    // cookie 删除失效
    del: function (name) {
        var date = new Date();
        date.setTime(date.getTime() - 10000);
        document.cookie = name + "=a; expires=" + date.toGMTString() + "; path=/";
    },
    getItem: function (name, item) {
        var json = eval("(" + this.get(name) + ")");
        return json[item];
    }
  };

  /**
   * 设置屏幕font-size
   **/
  Tool.setAdaption = function (baseFontsize) {
    baseFontsize = baseFontsize || 7.5;
    var docEl = document.documentElement;
    var refresh = function () {
        var dpr = window.devicePixelRatio,
            deviceWidth = docEl.getBoundingClientRect().width,
            deviceFontsize = deviceWidth / baseFontsize + "px";
        docEl.setAttribute('data-dpr', dpr);
        docEl.style.fontSize = deviceFontsize;
    };
    if (performance.navigation.type === 2) refresh();
    window.addEventListener('pageshow', function (e) {
        if (!e.pretersised) {
            refresh();
        }
    }, false);
    window.addEventListener('resize', function () {
        refresh();
    }, false);
  };

  // css
  Tool.css = function(element, attr){
    if(element.currentStyle){
      return element.currentStyle[attr];
    }else{
      return window.getComputedStyle(element,null)[attr];
    }
  };

  // getQueryString
  Tool.getQueryString = function (name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]);
      return null;
  };

  // 判断dom元素是否有类名
  Tool.hasClass = function functionName(obj, cls) {
    var obj_class = obj.className, //获取 class 内容.
        obj_class_lst = obj_class.split(/\s+/); //通过split空字符将cls转换成数组.
    for(var x in obj_class_lst) {
      if(obj_class_lst[x] == cls) { //循环数组, 判断是否包含cls
        return true;
      }
    }
    return false;
  };

  // dom元素添加类名
  Tool.addClass = function (obj, cls){
    var obj_class = obj.className,//获取 class 内容.
        blank = (obj_class != '') ? ' ' : '',//判断获取到的 class 是否为空, 如果不为空在前面加个'空格'.
        added = obj_class + blank + cls;//组合原来的 class 和需要添加的 class.
    obj.className = added;//替换原来的 class.
  }

  // dom元素删除类名
  function removeClass(obj, cls){
    var obj_class = ' '+obj.className+' ', //获取 class 内容, 并在首尾各加一个空格. ex) 'abc        bcd' -> ' abc        bcd '
        obj_class = obj_class.replace(/(\s+)/gi, ' '), //将多余的空字符替换成一个空格. ex) ' abc        bcd ' -> ' abc bcd '
        removed = obj_class.replace(' '+cls+' ', ' '); //在原来的 class 替换掉首尾加了空格的 class. ex) ' abc bcd ' -> 'bcd '
    removed = removed.replace(/(^\s+)|(\s+$)/g, ''); //去掉首尾空格. ex) 'bcd ' -> 'bcd'
    obj.className = removed;//替换原来的 class.
  }
})();
